package com.ruoyi.project.system.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.db.system.domain.SysRegion;
import com.ruoyi.db.system.domain.query.SysRegionQueryVo;
import com.ruoyi.project.system.service.SysRegionService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 行政区域Controller
 *
 * @author ruoyi
 * @date 2020-04-17
 */
@Api(tags="system管理：行政区域管理")
@RestController
@RequestMapping("/system/region")
public class SysRegionController extends BaseController
{
    @Autowired
    private SysRegionService sysRegionService;

    @ApiOperation("查询行政区域列表")
    @PreAuthorize("@ss.hasPermi('system:region:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysRegionQueryVo sysRegionQueryVo)
    {
        startPage();
        List<SysRegion> list = sysRegionService.selectSysRegionList(sysRegionQueryVo);
        return getDataTable(list);
    }

    @ApiOperation("导出行政区域列表")
    @Log(title = "行政区域", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('system:region:export')")
    @GetMapping("/export")
    public AjaxResult export(SysRegionQueryVo sysRegionQueryVo)
    {
        List<SysRegion> list = sysRegionService.selectSysRegionList(sysRegionQueryVo);
        ExcelUtil<SysRegion> util = new ExcelUtil<SysRegion>(SysRegion.class);
        return util.exportExcel(list, "region");
    }

    @ApiOperation("获取行政区域详细信息")
    @Log(title = "行政区域", businessType = BusinessType.OTHER)
    @PreAuthorize("@ss.hasPermi('system:region:query')")
    @GetMapping(value = "/{regionId}")
    public AjaxResult getInfo(@PathVariable("regionId") Long regionId)
    {
        return AjaxResult.success(sysRegionService.selectSysRegionById(regionId));
    }

    @ApiOperation("新增行政区域")
    @Log(title = "行政区域", businessType = BusinessType.INSERT)
    @PreAuthorize("@ss.hasPermi('system:region:add')")
    @PostMapping
    public AjaxResult add(@RequestBody SysRegion sysRegion)
    {
        return toAjax(sysRegionService.insertSysRegion(sysRegion));
    }

    @ApiOperation("修改行政区域")
    @Log(title = "行政区域", businessType = BusinessType.UPDATE)
    @PreAuthorize("@ss.hasPermi('system:region:edit')")
    @PutMapping
    public AjaxResult edit(@RequestBody SysRegion sysRegion)
    {
        return toAjax(sysRegionService.updateSysRegion(sysRegion));
    }

    @ApiOperation("删除行政区域")
    @Log(title = "行政区域", businessType = BusinessType.DELETE)
    @PreAuthorize("@ss.hasPermi('system:region:remove')")
	@DeleteMapping("/{regionIds}")
    public AjaxResult remove(@PathVariable Long[] regionIds)
    {
        return toAjax(sysRegionService.deleteSysRegionByIds(regionIds));
    }
}
